<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\BaseController;
use App\Http\Requests\Backstage\Link\CreateRequest;
use App\Http\Requests\Backstage\Link\UpdateRequest;
use App\Models\Link;
use App\Transformers\LinkTransformer;
use Illuminate\Http\Request;

class LinkController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $page_size = get_request_page();
        $models = Link::paginate($page_size);
        return $this->response->paginator($models, new LinkTransformer());
    }

    public function store(CreateRequest $request)
    {
        \DB::transaction(function () use ($request) {
            $attributes = $request->validated();
            $model = Link::create($attributes);
            return $model;
        });
        return $this->response->noContent();
    }

    public function show($id)
    {
        $model = Link::find($id);
        return $this->response->item($model, new LinkTransformer());
    }

    public function update(UpdateRequest $request, $id)
    {
        \DB::transaction(function () use ($request, $id) {
            $attributes = $request->validated();
            $model = Link::find($id);
            $model->update($attributes);
            return $model;
        });
        return $this->response->noContent();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
